<div class="row">
	<div class="col-xs-12">
		<div class="box">
			<div class="box-header">
				<h3 class="box-title">用户管理</h3>

				<div class="box-tools">
					<div class="input-group input-group-sm" style="width: 350px;">
						<input type="text" name="table_search" id="searchNameId"
							class="form-control pull-right" placeholder="用户名">

						<div class="input-group-btn">
							<button type="button" class="btn btn-default btn-search">
                                <i class="fa fa-search"></i>
							</button>
							<button type="button" class="btn btn-default btn-add">添加</button>
							<button type="button" class="btn btn-default btn-update">修改</button>
						</div>
					</div>
				</div>
			</div>
			<!-- /.box-header -->
			<div class="box-body table-responsive no-padding">
				<table class="table table-hover">
					<thead>
						<tr>
							<th>ID</th>
							<th>用户名</th>
							<th>部门</th>
							<th>邮箱</th>
							<th>手机号</th>
							<th>状态</th>
							<th>创建时间</th>
							<th>修改时间</th>
							<th>操作</th>
						</tr>
					</thead>
					<tbody id="tbodyId"></tbody>
				</table>
			</div>
			<div id="pageId" class="box-footer clearfix">
			</div>
			<!-- /.box-body -->
		</div>
		<!-- /.box -->
	</div>
</div>
<script type="text/javascript">

$(function(){
	$("#pageId").load("doPageUI.do",function(){
		doGetObjects();
	})
	$(".input-group-btn")
	   .on("click",".btn-search",doQueryObjects)
	   .on("click",".btn-add,.btn-update",doLoadEditUI)
	$("tbody").on("click",".btn-valid",doValidById);
})

function doLoadEditUI(){
	var title;
	if($(this).hasClass("btn-add")){
		$("#mainContentId").removeData();
		title="添加用户";
		var url="user/doUserSaveEditUI.do";
		$.ajax({url:url,success:function(result){
			console.log(result);
				if(result.state==0){
					alert(result.message);
				}else{
					doLoadtitle(title,url);
				}
			}
		})
	}else{
		title="修改用户";
		var rowData=doGetCheckedData();
		if(!rowData){
			alert("请先选择一个修改项");
			return;
		}
		$("#mainContentId").data("rowData",rowData);
		var url="user/doUserUpdateEditUI.do";
		$.ajax({url:url,success:function(result){
			console.log(result);
				if(result.state==0){
					alert(result.message);
				}else{
					doLoadtitle(title,url);
				}
			}
		})
	}
}
function doLoadtitle(title,url){
	$("#mainContentId").load(url,function(){
		$(".box-title").html(title);
	})
}
function doGetCheckedData(){
	var tr=$("#tbodyId input[type='radio']:checked").parents("tr");
	/* var roleId = doLoadUserRoleID(rowData.id); */
	var rowData=tr.data("rowData");
	return rowData;
}

/* function doLoadUserRoleID(userId){
	 var url="user/doFindRoleIdsByUserId.do";
	 var params
	 $.getJSON(url,params,function(){
		 
	 })
	 return 
} */

function doValidById(){
	   console.log("doValidById");
	   var url="user/doValidById.do";
	   var id=$(this).parents("tr").data("id");
	   var valid=$(this).parents("tr").data("valid");
	   var params={"id":id,"valid":valid?0:1};
       var btn=$(this);
	   $.post(url,params,function(result){
    	   if(result.state==1){
    		   alert(result.message);
    		   //doGetObjects();//重新查询刷新
    		   //优化方式实现
    		   doEditRow(btn,valid);
    	   }else{
    		   alert(result.message);
    	   }
       })    
   }    
   function doEditRow(obj,valid){
	   console.log(obj);
	   //1.修改按钮上的内容
	   $(obj).html(valid?"启用":"禁用");
	   //2.获取当前行tr对象，然后重新绑定新的值
	   var tr=$(obj).parents("tr");
	   tr.data("valid",valid?0:1);
	   //3.修改tr中第5个td中的内容(查询API文档)
	   tr.find("td:eq(5)").text(valid?"禁用":"启用");
   }

function doQueryObjects(){
	 $("#pageId").data("pageCurrent",1);
	   doGetObjects();
}
function doGetObjects(){
	var url="user/doFindPageObjects.do"
	var pageCurrent= $("#pageId").data("pageCurrent");
	if(!pageCurrent)
		pageCurrent=1;
	var params={"pageCurrent":pageCurrent}
	params.username=$("#searchNameId").val();
	console.log(params.username);
	console.log(params);
	$.post(url,params,function(result){
		if(result){
			console.log(result)
			if(result.data!=null){
				doSetTable(result.data.records);
				doSetPagination(result.data);
			}else{
				alert(result.message);
			}
		}else{
			alert(result.message)
		}
	})
}
function doSetTable(data){
	//1.获取tbody对象，并清空内容
	  var tBody=$("#tbodyId");
	  tBody.empty();
	  //2.迭代data内容将数据追加到tbody中
	  for(var i in data){
		  var tr=$("<tr></tr>");
		  tr.data("id",data[i].id);
		  tr.data("valid",data[i].valid);
		  tr.data("rowData",data[i]);
		  console.log(data[i].sysDept.id);
		  var tds="<td><input type='radio' name='radioId' value='"+data[i].id+"' ></td>"+
		     "<td>"+data[i].username+"</td>"+
		     "<td>"+data[i].sysDept.name+"</td>"+
		     "<td>"+data[i].email+"</td>"+
		     "<td>"+data[i].mobile+"</td>"+
		     "<td>"+(data[i].valid?"启用":"禁用")+"</td>"+
		     "<td>"+data[i].createdTime+"</td>"+
		     "<td>"+data[i].modifiedTime+"</td>"+
		     "<td><button type='button' class='btn btn-default btn-valid'>"+(data[i].valid?"禁用":"启用")+"</button></td>";
		 tr.append(tds);
		 tBody.append(tr);
	  }
}
 /*   $(document).ready(function(){
	   $("#pageId").load("pageUI.do",function(){
		   doGetObjects();
	   });
	   $(".input-group-btn")
	   .on("click",".btn-search",doQueryObjects)
	   .on("click",".btn-add,.btn-update",doAddOrUpdate);
	   $("tbody").on("click",".btn-valid",doValidById);
   });
   
   function doAddOrUpdate(){
	   //1.判定点击的对象
	   var title;
	   if($(this).hasClass("btn-add")){
		   title="添加用户";
		   doLoadEditPage(title);
	   }else if($(this).hasClass("btn-update")){
		   title="修改用户";
		   var id=getCheckedId();
		   console.log("id="+id)
		   $(".container-fluid").data("id",id);
		  
		   doLoadEditPage(title);
	   }
   }
   function getCheckedId(){
	   return $("tbody input[name='radioId']:checked").val();
   }
   
   function doLoadEditPage(title){
	   var url="user/editUI.do"
	   $(".container-fluid").load(url,function(){
		   $(".box-title").html(title);
	   }) 
   }
   
   function doValidById(){
	   console.log("doValidById");
	   var url="user/doValidById.do";
	   var id=$(this).parents("tr").data("id");
	   var valid=$(this).parents("tr").data("valid");
	   var params={"id":id,"valid":valid?0:1};
       var btn=$(this);
	   $.post(url,params,function(result){
    	   if(result.state==1){
    		   alert(result.message);
    		   //doGetObjects();//重新查询刷新
    		   //优化方式实现
    		   doEditRow(btn,valid);
    	   }else{
    		   alert(result.message);
    	   }
       })    
   }    
   function doEditRow(obj,valid){
	   console.log(obj);
	   //1.修改按钮上的内容
	   $(obj).html(valid?"启用":"禁用");
	   //2.获取当前行tr对象，然后重新绑定新的值
	   var tr=$(obj).parents("tr");
	   tr.data("valid",valid?0:1);
	   //3.修改tr中第5个td中的内容(查询API文档)
	   tr.find("td:eq(4)").text(valid?"禁用":"启用");
   }
   //处理查询按钮事件
   function doQueryObjects(){
	   $(".pagination").data("pageCurrent",1);
	   doGetObjects();
   }
   //页面加载完成异步加载当前页数据
   function doGetObjects(){
	   var url="user/doFindPageObjects.do";
	   var pageCurrent=
	   $(".pagination").data("pageCurrent");
	   if(!pageCurrent)pageCurrent=1;
	   var params={"pageCurrent":pageCurrent}
	   params.username=$("#searchNameId").val().trim();
	   console.log(params);
	   $.getJSON(url,params,function(result){
		   if(result.state==1){
			  setTableBodyRows(result.data.records);
			  setPagination(result.data);
		   }else{
			  alert(result.message);
		   }
	   });
   }
   //通过服务端返回的数据初始化页面
   function setTableBodyRows(data){
	  //1.获取tbody对象，并清空内容
	  var tBody=$("#tbodyId");
	  tBody.empty();
	  //2.迭代data内容将数据追加到tbody中
	  for(var i in data){
		  var tr=$("<tr></tr>");
		  tr.data("id",data[i].id);
		  tr.data("valid",data[i].valid);
		  var tds="<td><input type='radio' name='radioId' value='"+data[i].id+"' ></td>"+
		     "<td>"+data[i].username+"</td>"+
		     "<td>"+data[i].deptName+"</td>"+
		     "<td>"+data[i].email+"</td>"+
		     "<td>"+data[i].mobile+"</td>"+
		     "<td>"+(data[i].valid?"启用":"禁用")+"</td>"+
		     "<td>"+data[i].createdTime+"</td>"+
		     "<td>"+data[i].modifiedTime+"</td>"+
		     "<td><button type='button' class='btn btn-default btn-valid'>"+(data[i].valid?"禁用":"启用")+"</button></td>";
		 tr.append(tds);
		 tBody.append(tr);
	  }
	
   } */
</script>




